import { _decorator, Component, Node,log,v2, v3,Vec2,Animation,TweenAction, Tween, Vec3 } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {

    @property({type: Animation})
    public BodyAnim: Animation | null = null;



    private _startJump: boolean = false;

    // 跳跃步长
    private _jumpStep: number = 0;
    // 当前跳跃时间
    private _curJumpTime: number = 0;
    // 每次跳跃时长
    private _jumpTime: number = 0.3;
    // 当前跳跃速度
    private _curJumpSpeed: number = 0;

    public canJump:boolean=true;

    public index: number; // 当前跳到第几格

    public init() {
        this.index=0;
    
    }

public jump(step: number) {
    log("jump"+step)


   // this.scheduleOnce(() => {
   //     this.menuAnim.play('menu_intro');
   // }, 0.5);

   
   const tw1 = new Tween(this.node)
   //.to(3, new V2(10, 10), { easing: 'bounceInOut' })
   //.to(1, { scale: new Vec3(3, 3, 3) }, { easing: 'bounceInOut' })

   let x=this.node.position.x;
   let y=this.node.position.y;
  
   if(step==2){
    x=x+400;
   }else{
    x=x+200;
   }
   tw1.to(0.01, { position: new Vec3(x, y, 0) })
  // .to(1, { scale: new Vec3(1, 1, 1) }, { easing: 'bounceInOut' })
   //.union()
   //.repeat(2) // 执行 2 次
   .start();



    if (this.BodyAnim) {
        if (step === 1) {
           this.BodyAnim.play("oneStep");
            log('oneStep');
           // this.BodyAnim.
            
            //this.BodyAnim.play();
        } else if (step === 2) {
            this.BodyAnim.play("oneStep");
            log('oneStep');
        }
    }else{
        log('anim is null');
    }
    
     if (this._startJump) {
            return;
        }
        this._startJump = true;

        this._jumpStep = step*10;
        this._curJumpTime = 0;
        this._curJumpSpeed = this._jumpStep / this._jumpTime;
       
        //this._curJumpSpeed = this._jumpStep / this._jumpTime;
        //this.node.getPosition();
        //this.node.getPosition(this._curPos);
       // Vec2.add(this._targetPos, this._curPos, new Vec2(this._jumpStep, 0));
}

update (deltaTime: number) {

    /*
    if(this._startJump){
       var ty= this.node.position.y;
       var tx= this.node.position.x+5;
       this.node.setPosition(tx,ty);
       this._startJump = false;
    }
    */


    if (this._startJump) {
        this._curJumpTime += deltaTime;
        if (this._curJumpTime > this._jumpTime) {
            // end
           // this.node.setPosition(this._targetPos);
            //this.node.setPosition(this._targetPos.x,this._targetPos.y)
            this._startJump = false;
        } else {
           
            let tempy = this._curJumpSpeed * deltaTime;

           // var ty= this.node.position.y;
           // var tx= this.node.position.x+tempy;
           // this.node.setPosition(tx,ty);
            
           
        }
    }


    //log(deltaTime)
    /*
    if (this._startJump) {
        this._curJumpTime += deltaTime;
        if (this._curJumpTime > this._jumpTime) {
            // end
           // this.node.setPosition(this._targetPos);
            //this.node.setPosition(this._targetPos.x,this._targetPos.y)
            this._startJump = false;
        } else {
           
            let tempy = this._curJumpSpeed * deltaTime;
            this.node.position.y=tempy;
            
            this.node.setPosition(0,tempy)
        }
    }
    */
}



    public die() {
        log('im die');

        const tw1 = new Tween(this.node);
      

        let x=this.node.position.x;
        let y=this.node.position.y;
        tw1.to(0.5, { position: new Vec3(x, -500, 0) })
        .start();



    }

   
}

